package com.example.tests;

import static com.example.fw.RandomGenerator.generateAddress;
import static com.example.fw.RandomGenerator.generateEmail;
import static com.example.fw.RandomGenerator.generateHomePhone;
import static com.example.fw.RandomGenerator.generateLimitedNumber;
import static com.example.fw.RandomGenerator.generateMobilePhone;
import static com.example.fw.RandomGenerator.generateNumber;
import static com.example.fw.RandomGenerator.generateString;
import static com.example.fw.RandomGenerator.generateYear;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.thoughtworks.xstream.XStream;

public class ContactDataGenerator {

	public static void main(String[] args) throws IOException {
		if (args.length < 3) {
			System.out.println("Please specify perameters: <amount of test data> <file> <format>");
			return;
		}
		
		int amount = Integer.parseInt(args[0]);
		File file = new File(args[1]);
		String format = args[2];
		
		if (file.exists()){
			System.out.println("File exists, please remove it manually: "+file);
		}
			
		List<ContactData> Contacts = generateRandomContacts(amount);
		if (format.equals("csv")) {
			saveContactsToCsvFile(Contacts,file);
		} else if (format.equals("xml")) {
			saveContactsToXmlFile(Contacts,file);
		} else {
			System.out.println("Uknown format "+format);
		}
	}

	private static void saveContactsToXmlFile(List<ContactData> Contacts, File file) throws IOException {
		XStream xs = new XStream();
		xs.alias("Contact", ContactData.class);
		String xml = xs.toXML(Contacts);
		FileWriter writer = new FileWriter(file);
		writer.write(xml);
		writer.close();
	}
	
	public static List<ContactData> loadContactsFromXmlFile(File file) {
		XStream xs = new XStream();
		xs.alias("Contact", ContactData.class);
		return (List<ContactData>) xs.fromXML(file);
	}

	private static void saveContactsToCsvFile(List<ContactData> Contacts, File file) throws IOException {
		FileWriter writer = new FileWriter(file);
		for (ContactData Contact : Contacts) {
			writer.write(Contact.getFirstName()+","+Contact.getLastName()+","+Contact.getAddress()+","+Contact.getPhoneHome()+","+Contact.getPhoneMobile()+","+Contact.getPhoneWork()+","+Contact.getEmail1()+","+Contact.getEmail2()+","+Contact.getDayOfBirth()+","+Contact.getMonthOfBirth()+","+Contact.getYearOfBirth()+","+Contact.getAddress2()+","+Contact.getPhoneHome2()+",!\n");
		}
		writer.close();
	}

	public static List<ContactData> generateRandomContacts(int amount) {
		List<ContactData> list = new ArrayList<ContactData>();
		for (int i = 0; i < amount; i++){
			ContactData contact = new ContactData()
				.withFirstName(generateString())
				.withLastName(generateString())
				.withAddress(generateAddress())
				.withPhoneHome(generateHomePhone())
				.withPhoneMobile(generateMobilePhone())
				.withPhoneWork(generateNumber())
				.withEmail1(generateEmail())
				.withEmail2(generateEmail())
				.withDayOfBirth(generateLimitedNumber(30))
				.withMonthOfBirth(generateLimitedNumber(11))
				.withYearOfBirth(generateYear())
				.withAddress2(generateAddress())
				.withPhoneHome2(generateHomePhone());   			
			list.add(contact);
		}		
		return list;
	}	
	
	public static List<ContactData> loadContactsFromCsvFile(File file) throws IOException {
		List<ContactData> list = new ArrayList<ContactData>();
		FileReader reader = new FileReader(file);
		BufferedReader bufferedReder = new BufferedReader(reader);
		String line = bufferedReder.readLine();
		while (line != null){
			String[] part = line.split(",");
			ContactData contact = new ContactData()
				.withFirstName(part[0])
				.withLastName(part[1])
				.withAddress(part[2])
				.withPhoneHome(part[3])
				.withPhoneMobile(part[4])
				.withPhoneWork(part[5])
				.withEmail1(part[6])
				.withEmail2(part[7])
				.withDayOfBirth(part[8])
				.withMonthOfBirth(part[9])
				.withYearOfBirth(part[10])
				.withAddress2(part[11])
				.withPhoneHome2(part[12]);  			
			list.add(contact);
			FileWriter writer = new FileWriter(new File("1.txt"));
			writer.write(contact.getFirstName()+","+contact.getLastName()+","+contact.getAddress()+","+contact.getPhoneHome()+","+contact.getPhoneMobile()+","+contact.getPhoneWork()+","+contact.getEmail1()+","+contact.getEmail2()+","+contact.getDayOfBirth()+","+contact.getMonthOfBirth()+","+contact.getYearOfBirth()+","+contact.getAddress2()+","+contact.getPhoneHome2()+",!\n");
			writer.close();
			line = bufferedReder.readLine();
		}
		bufferedReder.close();
		return list;
	}

}
